<template>
    <div class="provide-inject">
        <h1>provide / inject</h1>
        <div ref="BarChartRef" class="echart"></div>
    </div>
</template>
<script lang="ts" setup>
import { ref, onMounted, inject } from 'vue'
import { ECInit, ECOption } from '@/utils/echarts'
const BarChartRef = ref<HTMLElement>()
onMounted(() => {
    // 注入echarts实例
    const echarts = inject("echarts") as ECInit;
    // 基于准备好的dom，初始化echarts实例
    if (BarChartRef.value) {
        const barChart = echarts.init(BarChartRef.value);
        const option: ECOption = {
            title: {
                text: '柱状图'
            },
            tooltip: {},
            xAxis: {
                data: ['衬衫', '羊毛衫', '雪纺衫', '裤子', '高跟鞋', '袜子']
            },
            yAxis: {},
            series: [
                {
                    name: '销量',
                    type: 'bar',
                    data: [5, 20, 36, 10, 10, 20]
                }
            ]
        }
        // 绘制图表
        barChart.setOption(option);
    }
})
</script>
<style lang="scss" scoped>
.provide-inject {
    margin: auto;
    max-width: 60vw;

    .echart {
        width: 100%;
        height: 500px;
    }
}
</style>